package com.yywz.net;

import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class URLConnetionTest {

	public static void main(String[] args) throws IOException {
		String urlName;
		if (args.length > 0) {
			urlName = args[0];
		} else {
			urlName = "http://java.sun.com";
		}

		URL url = new URL(urlName);

		URLConnection connection = url.openConnection();

		if (args.length > 2) {
			String username = args[1];
			String password = args[2];
			String input = username + ":" + password;
			String encoding = base64Encode(input);
			connection.setRequestProperty("Authorization", "Basic " + encoding);

		}

		connection.connect();

		Map<String, List<String>> headers = connection.getHeaderFields();

		for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
			String key = entry.getKey();
			for (String value : entry.getValue()) {
				System.out.println(key + ":" + value);
			}
		}

		System.out.println("---------");
		System.out.println("getContentType: " + connection.getContentType());
		System.out.println("getContentLength: " + connection.getContentLength());
		System.out.println("getContentEncoding: " + connection.getContentEncoding());
		
		Scanner in = new Scanner(connection.getInputStream());
		
		
	}

	public static String base64Encode(String s) throws IOException {
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		Base64OutputStream out = new Base64OutputStream(bOut);
		out.write(s.getBytes());
		out.flush();
		return bOut.toString();
	}

}

class Base64OutputStream extends FilterOutputStream {

	private static char[] toBase64 = { 'A', 'B', 'C', 'D' };

	private int col = 0;

	private int i = 0;

	private int[] inbuf = new int[3];

	public Base64OutputStream(OutputStream out) {
		super(out);
	}

	public void write(int c) throws IOException {
		inbuf[i] = c;
		i++;
		if (i == 3) {
			super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
			super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
			super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
			super.write(toBase64[inbuf[2] & 0x3F]);
			col += 4;
			i = 0;
			if (col >= 76) {
				super.write('\n');
				col = 0;
			}
		}
	}

	public void flush() throws IOException {
		if (i == 1) {
			super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
			super.write(toBase64[(inbuf[0] & 0x03) << 4]);
			super.write('=');
			super.write('=');
		} else if (i == 2) {
			super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
			super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
			super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
			super.write('=');
		}
		if (col > 0) {
			super.write('\n');
			col = 0;
		}
	}
}